import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

enum SortType {
  byTime,
  byDuration,
}

enum FilterType {
  showFavorites,
  showAdminVisible,
  showAllMembersVisible,
  showSelectedMembersVisible,
}

class SortFilterDialog extends StatefulWidget {
  final SortType? initialSortType;
  final FilterType? initialFilterType;
  final Function(SortType?, FilterType?) onApply;

  const SortFilterDialog({
    Key? key,
    this.initialSortType,
    this.initialFilterType,
    required this.onApply,
  }) : super(key: key);

  @override
  State<SortFilterDialog> createState() => _SortFilterDialogState();
}

class _SortFilterDialogState extends State<SortFilterDialog> {
  late SortType? selectedSortType;
  late FilterType? selectedFilterType;

  @override
  void initState() {
    super.initState();
    selectedSortType = widget.initialSortType;
    selectedFilterType = widget.initialFilterType;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20.r),
          topRight: Radius.circular(20.r),
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // _buildHeader(),
          // Divider(height: 1, color: Colors.grey.shade200),
          _buildSortOptions(),
          Divider(height: 1, color: Colors.grey.shade200),
          _buildFilterOptions(),
          // SizedBox(height: 20.h),
          // _buildCancelButton(),
          SizedBox(height: MediaQuery.of(context).padding.bottom),
        ],
      ),
    );
  }

  Widget _buildHeader() {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 16.h),
      child: Center(
        child: Text(
          '排序',
          style: TextStyle(
            fontSize: 18.sp,
            fontWeight: FontWeight.w500,
            color: Colors.black,
          ),
        ),
      ),
    );
  }

  Widget _buildSortOptions() {
    return Column(
      children: [
        _buildOptionItem(
          title: '按时间排序',
          isSelected: selectedSortType == SortType.byTime,
          onTap: () {
            setState(() {
              selectedSortType = SortType.byTime;
            });
            widget.onApply(selectedSortType, selectedFilterType);
            Get.back();
          },
        ),
        _buildOptionItem(
          title: '按录音长度排序',
          isSelected: selectedSortType == SortType.byDuration,
          onTap: () {
            setState(() {
              selectedSortType = SortType.byDuration;
            });
            widget.onApply(selectedSortType, selectedFilterType);
            Get.back();
          },
        ),
      ],
    );
  }

  Widget _buildFilterOptions() {
    return Column(
      children: [
        _buildOptionItem(
          title: '只显示收藏',
          isSelected: selectedFilterType == FilterType.showFavorites,
          onTap: () {
            setState(() {
              selectedFilterType = FilterType.showFavorites;
            });
            widget.onApply(selectedSortType, selectedFilterType);
            Get.back();
          },
        ),
        _buildOptionItem(
          title: '只显示管理员可见',
          isSelected: selectedFilterType == FilterType.showAdminVisible,
          onTap: () {
            setState(() {
              selectedFilterType = FilterType.showAdminVisible;
            });
            widget.onApply(selectedSortType, selectedFilterType);
            Get.back();
          },
        ),
        _buildOptionItem(
          title: '只显示所有成员可见',
          isSelected: selectedFilterType == FilterType.showAllMembersVisible,
          onTap: () {
            setState(() {
              selectedFilterType = FilterType.showAllMembersVisible;
            });
            widget.onApply(selectedSortType, selectedFilterType);
            Get.back();
          },
        ),
        _buildOptionItem(
          title: '只显示部分成员可见',
          isSelected: selectedFilterType == FilterType.showSelectedMembersVisible,
          onTap: () {
            setState(() {
              selectedFilterType = FilterType.showSelectedMembersVisible;
            });
            widget.onApply(selectedSortType, selectedFilterType);
            Get.back();
          },
        ),
      ],
    );
  }

  Widget _buildOptionItem({
    required String title,
    required bool isSelected,
    required VoidCallback onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        width: double.infinity,
        padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.h),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 16.sp,
                color: Colors.black87,
              ),
            ),
            // if (isSelected)
            //   Icon(
            //     Icons.check,
            //     color: Colors.blue,
            //     size: 20.r,
            //   ),
          ],
        ),
      ),
    );
  }

  Widget _buildCancelButton() {
    return GestureDetector(
      onTap: () => Get.back(),
      child: Container(
        width: 300.w,
        height: 50.h,
        decoration: BoxDecoration(
          color: Colors.grey.shade200,
          borderRadius: BorderRadius.circular(25.r),
        ),
        alignment: Alignment.center,
        child: Text(
          '取消',
          style: TextStyle(
            fontSize: 16.sp,
            color: Colors.black87,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }
} 